﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using Sunboxsoft.Common;

namespace Sunboxsoft.ASMS.Client.Domain.LocalService
{
    public class QueryService
    {
        /// <summary>
        /// 获取主数据项目列表
        /// </summary>
        /// <param name="masterDataCode"></param>
        /// <param name="parentCode"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public ListResult<WebRef.EMasterDataItem> QueryMasterDataItemList(string masterDataCode, string parentCode, int? tag)
        {
            using (SQLiteConnection connection = new SQLiteConnection(SQLiteDatabase.ConnectionString))
            {
                connection.Open();

                SQLiteCommand command = new SQLiteCommand(connection);

                Dictionary<string, object> paramDict = new Dictionary<string, object>();

                paramDict.Add("MasterDataCode", masterDataCode);
                if (!string.IsNullOrEmpty(parentCode))
                {
                    paramDict.Add("ParentCode", parentCode);
                }
                if (tag != null)
                {
                    paramDict.Add("Tag", tag.Value);
                }


                SQLiteDatabase.BuildSQLCondition(command, "select * from ASMS_MasterDataItem ", paramDict, "order by SortIndex asc");


                List<WebRef.EMasterDataItem> list = new List<WebRef.EMasterDataItem>();
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        WebRef.EMasterDataItem entity = new WebRef.EMasterDataItem();
                        entity.MasterDataCode = Convert.ToString(reader["MasterDataCode"]);
                        entity.ParentCode = Convert.ToString(reader["ParentCode"]);
                        entity.Code = Convert.ToString(reader["Code"]);
                        entity.Name = Convert.ToString(reader["Name"]);
                        entity.Pinyin = Convert.ToString(reader["Pinyin"]);
                        entity.Letter = Convert.ToString(reader["Letter"]);

                        list.Add(entity);
                    }
                }
                return new ListResult<WebRef.EMasterDataItem>(list);
            }
        }

    }
}
